<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "Design".
 */
class Design extends ActiveRecord {

    // 状态
    const StatusNormal = 0;
    public static $statusSelector = [
        self::StatusNormal => ['title' => '正常', 'status' => 'blue'],
    ];

    // 上线设备
    const DevicePC = 1;
    const DeviceMobile = 2;
    public static $deviceSelector = [
        self::DevicePC => ['title' => 'PC端', 'status' => 'blue'],
        self::DeviceMobile => ['title' => '移动端', 'status' => 'orange'],
    ];

    // 作品等级
    const LevelD = 0;
    const LevelC = 1;
    const LevelB = 2;
    const LevelA = 3;
    const LevelS = 4;
    public static $levelSelector = [
        self::LevelD => ['title' => '游刃有余', 'status' => 'gray'],
        self::LevelC => ['title' => '炉火纯青', 'status' => 'orange'],
        self::LevelB => ['title' => '技压群雄', 'status' => 'blue'],
        self::LevelA => ['title' => '出神入化', 'status' => 'green'],
        self::LevelS => ['title' => '登峰造极', 'status' => 'red'],
    ];

    // 上线平台
    const PlatformOther = 0;
    const PlatformTaobao = 1;
    const PlatformTianmao = 2;
    const PlatformAlibaba = 3;
    const PlatformJingdong = 4;
    const PlatformWeipinhui = 5;
    const PlatformDangdang = 6;
    const PlatformYihaodian = 7;
    const PlatformAmazon = 8;
    const PlatformMogujie = 9;
    const PlatformMeilishuo = 10;
    const PlatformApp = 11;
    public static $platformSelector = [
        self::PlatformTaobao => ['title' => '淘宝', 'status' => 'orange'],
        self::PlatformTianmao => ['title' => '天猫', 'status' => 'red'],
        self::PlatformAlibaba => ['title' => '阿里巴巴', 'status' => 'blue'],
        self::PlatformJingdong => ['title' => '京东', 'status' => 'red'],
        self::PlatformWeipinhui => ['title' => '唯品会', 'status' => 'red'],
        self::PlatformDangdang => ['title' => '当当', 'status' => 'red'],
        self::PlatformYihaodian => ['title' => '一号店', 'status' => 'red'],
        self::PlatformAmazon => ['title' => '亚马逊', 'status' => 'blue'],
        self::PlatformMogujie => ['title' => '蘑菇街', 'status' => 'blue'],
        self::PlatformMeilishuo => ['title' => '美丽说', 'status' => 'blue'],
        self::PlatformApp => ['title' => 'APP', 'status' => 'blue'],
        self::PlatformOther => ['title' => '其他', 'status' => 'blue'],
    ];

    // 风格
    public static $styleSelector = [
        '1' => '时尚简约', '2' => '扁平化', '3' => '卡通风', '4' => '科技风',
        '5' => '中国风', '6' => '质感炫酷', '7' => '古典欧式', '8' => '日韩小清新',
    ];

    // only define rules for those attributes that
    // will receive user inputs.
    public function rules()
    {
        return [
            [['designer_id', 'category_id', 'device', 'platform', 'model', 'thumb', 'image'], 'required'],
            [['designer_id', 'category_id', 'device', 'level', 'platform', 'model', 'status', 'view', 'collection', 'deleted_at'], 'integer'],
            [['title'], 'string', 'max' => 64],
            [['style'], 'string', 'max' => 32],
            [['thumb', 'image'], 'string', 'max' => 128],
            [['remark'], 'string', 'max' => 255],
        ];
    }
    /**
     * 字段名称
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'title' => '作品名称',
            'designer_id' => '设计师',
            'category_id' => '分类',
            'device' => '上线设备',
            'level' => '作品等级',
            'platform' => '上线平台',
            'model' => '设计类型',
            'style' => '设计风格',
            'thumb' => '缩略图',
            'image' => '作品',
            'view' => '查看次数',
            'collection' => '收藏数量',
            'status' => '状态',
            'remark' => '备注',
            'deleted_at' => '删除时间',
        ];
    }
    /**
     * update & insert data check config for html
     * @param $type string 页面操作类型
     * @return array
     */
    public static function flyer($type = 'update')
    {
        // just search
        $rule = [
            'param' => [
                'title' => ['作品名称', ['maxlength' => 255, 'required']],
                'device' => ['上线设备', ['inkey' => self::$deviceSelector, 'required']],
                'platform' => ['上线平台', ['inkey' => self::$platformSelector, 'required']],
                'model' => ['设计类型', ['int', 'required']],
                // 'level' => ['作品等级', ['inkey' => self::$levelSelector, 'required']],
                'style' => ['设计风格', ['inkey' => self::$styleSelector, 'required']],
                'category_id' => ['分类', ['int', 'required']],
                // 'thumb' => ['缩略图', ['maxlength' => 128, 'required']],
                // 'image' => ['图片', ['maxlength' => 128, 'required']],
                // 'collection' => ['收藏量', ['int', 'required']],
                // 'view' => ['浏览量', ['int', 'required']],
                // 'status' => ['状态', ['int', 'required']],
                'remark' => ['备注', ['maxlength' => 255]],
            ],
        ];
        return $rule;
    }

    /**
     * 获取user信息
     * @return \yii\db\ActiveQuery
     */
    public function getDesigner()
    {
        return $this->hasOne(User::className(), ['id' => 'designer_id']);
    }

    /**
     * 增加浏览次数
     * @return boolean
     */
    public function browser()
    {
        $this->view = $this->view + 1;
        return $this->save();
    }

    /**
     * 增加收藏次数
     * @return boolean
     */
    public function collection($is = true)
    {
        $this->collection = $this->collection + ($is ? 1 : -1);
        if($this->collection < 0) {
            $this->collection = 0;
        }
        return $this->save();
    }
}